﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.IO;

using System.Xml;
using System.Xml.Serialization;
using TouchRC.MetadataCaching.Extensions;
using TouchRC.MetadataCaching;

namespace MediaScoutGUI.GUITypes
{
    public class Movie : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        private DirectoryInfo m_Filepath;
        private string m_Name;
        private bool m_HasMetadata = false;
        private bool m_HasPoster = false;
        private bool m_HasBackdrop = false;
        private String m_Desc;
        private string m_Id;

        public String Description
        {
            get { return m_Desc; }
            set { m_Desc = value; }
        }

        public string Id
        {
            get { return m_Id; }
            set { m_Id = value; }
        }

        public bool HasPoster
        {
            get { return m_HasPoster; }
            set
            {
                m_HasPoster = value;
                NotifyPropertyChanged("HasPoster");
            }
        }

        public bool HasBackdrop
        {
            get { return m_HasBackdrop; }
            set
            {
                m_HasBackdrop = value;
                NotifyPropertyChanged("HasBackdrop");
            }
        }

        public bool HasMetadata
        {
            get { return m_HasMetadata; }
            set { 
                m_HasMetadata = value;
                NotifyPropertyChanged("HasMetadata");
            }
        }

        public DirectoryInfo Filepath
        {
            get { return m_Filepath; }
            set { 
                m_Filepath = value;
                NotifyPropertyChanged("Filepath");
            }
        }

        public string Name
        {
            get { return m_Name; }
            set { 
                m_Name = value;
                NotifyPropertyChanged("Name");
            }
        }

        public List<FileInfo> AssociatedMediaFiles
        {
            get
            {
                List<FileInfo> assocFiles = new List<FileInfo>();
                foreach (string fileType in CommonHelper.FileTypes)
                {
                    FileInfo[] currFiles = m_Filepath.GetFiles("*" + fileType);
                    if (currFiles.Length > 0)
                    {
                        foreach (FileInfo currFile in currFiles)
                        {
                            assocFiles.Add(currFile);
                        }
                    }
                }

                return assocFiles;
            }
        }

        public Movie(FileInfo filePath)
        {
            ParseMovie(filePath);
        }

        public Movie(DirectoryInfo filepath)
        {
            m_Filepath = filepath;
            FileInfo myMoviesXml = m_Filepath.GetMyMoviesXml();

            ParseMovie(myMoviesXml);
        }

        private void ParseMovie(FileInfo filePath)
        {
            m_Filepath = filePath.Directory;

            if (File.Exists(filePath.FullName))
            {
                m_Name = filePath.Name;

                XmlSerializer s = new XmlSerializer(typeof(MediaScout.Movie));
                TextReader w = new StreamReader(filePath.FullName);
                moviebase = (MediaScout.Movie)s.Deserialize(w);
                w.Close();

                NotifyPropertyChanged("Moviebase.Rating");

                m_HasMetadata = true;
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(filePath.FullName);
                XmlNode node = xdoc.DocumentElement;
                m_Name = node.SelectSingleNode("LocalTitle").InnerText;
                m_Desc = node.SelectSingleNode("Description").InnerText;

                string id = string.Empty;


                if (node.SelectSingleNode("IMDB") != null &&
                    !string.IsNullOrEmpty(node.SelectSingleNode("IMDB").InnerText))
                    m_Id = node.SelectSingleNode("IMDB").InnerText;
                else if (node.SelectSingleNode("TMDbId") != null)
                    m_Id = node.SelectSingleNode("TMDbId").InnerText;
                else if (node.SelectSingleNode("ID") != null)
                    m_Id = node.SelectSingleNode("ID").InnerText;
            }

            if (File.Exists(m_Filepath.GetFolderPoster().FullName))
                m_HasPoster = true;
            if (File.Exists(m_Filepath.GetFolderBackdrop().FullName))
                m_HasBackdrop = true;
        }

        private MediaScout.Movie moviebase;

        public MediaScout.Movie Moviebase
        {
            get { return moviebase; }
            set { moviebase = value; }
        }

        public override String ToString()
        {
            return this.Name;
        }
    }
}
